package com.zhao.integration.config;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

/**
 * message service config
 * 
 * @author zhaoyongfei
 *
 */
@Configuration
//@EnableRabbit
@EnableJms
//public class JmsConfig implements JmsListenerConfigurer {
public class JmsConfig {

	public static String DEFAULT_DESTINATION_NAME = "default_destination";

	//		@Override
	//		public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
	//			SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	//			endpoint.setId("myJmsEndpoint");
	//			endpoint.setDestination(DEFAULT_DESTINATION_NAME);
	//			endpoint.setMessageListener(messageListener());
	//			registrar.registerEndpoint(endpoint);
	//		}

	@Bean
	public DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory() {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		factory.setConnectionFactory(connectionFactory());
		factory.setConcurrency("5");
		return factory;
	}

	//	@Bean
	//	public AmqpTemplate rabbitTemplate() {
	//		RabbitTemplate template = new RabbitTemplate(connectionFactory());
	//		return template;
	//	}

	@Bean
	public ConnectionFactory connectionFactory() {
		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
		return connectionFactory;
	}

	@Bean
	public JmsTemplate jmsTemplate() {
		JmsTemplate jmsTemplate = new JmsTemplate();
		jmsTemplate.setConnectionFactory(connectionFactory());
		jmsTemplate.setDefaultDestinationName(DEFAULT_DESTINATION_NAME);
		return jmsTemplate;
	}

	//	@Bean
	//	public MessageListenerContainer messageListenerContainer() {
	//		DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
	//		messageListenerContainer.setConnectionFactory(connectionFactory());
	//		messageListenerContainer.setMessageListener(messageListener());
	//		Destination destination = new ActiveMQQueue("DEFAULT_DESTINATION_NAME");
	//		messageListenerContainer.setDestination(destination);
	//		messageListenerContainer.setSessionTransacted(true);
	//		return messageListenerContainer;
	//	}

	//	@Bean
	//	public MessageListener messageListener() {
	//		MessageListener messageListener = new MessageListener() {
	//			@Override
	//			public void onMessage(Message message) {
	//				if (message instanceof TextMessage) {
	//					TextMessage textMessage = (TextMessage) message;
	//					try {
	//						System.out.println(textMessage.getText());
	//					} catch (JMSException e) {
	//						e.printStackTrace();
	//					}
	//				} else {
	//					System.out.println(message);
	//				}
	//			}
	//		};
	//		return messageListener;
	//	}
}
